from coin.exchange.ftx.kr_rest.currency import FTXCurrency
from coin.exchange.ftx.kr_rest.native_private_client import FTXNativePrivateClient
import coin.exchange.base.kr_rest.private_client_base as privcb
from coin.proto.coin_query_pb2 import (AccountBalance, CurrencyBalance)


class FTXPrivateParser(object):
  @staticmethod
  def parse_account_balance(update_msg):
    assert update_msg.get('success') is True
    balance_info = update_msg['result']
    currency_balance_list = []
    for balance_detail in balance_info:
      native_currency = balance_detail['coin']
      currency = FTXCurrency.FromStrNativeCurrency(native_currency)
      total = balance_detail['total']
      available = balance_detail['free']
      currency_balance = CurrencyBalance(currency=currency.currency,
                                         currency_native=currency.native_currency,
                                         total=total,
                                         available=available)
      currency_balance_list.append(currency_balance)
    return AccountBalance(
        exchange='Ftx',
        each_balance=currency_balance_list,
    )


class FTXPrivateClient(privcb.PrivateClientBase):
  def __init__(self, key_file, timeout=20):
    privcb.PrivateClientBase.__init__(self)
    self.nprivc = FTXNativePrivateClient(key_file=key_file, timeout=timeout)

  def query_account_balance_impl(self):
    update = self.nprivc.query_account_balance()
    update.msg = FTXPrivateParser.parse_account_balance(update.msg)
    return update

  def query_list_orders_impl(self, product):
    return NotImplementedError

  def query_cancel_product_impl(self, product):
    raise NotImplementedError

  def query_fills_impl(self, product):
    raise NotImplementedError

  def query_submit_impl(self, product, side, price, qty):
    raise NotImplementedError
